package io.github.yotta.logger.manager;


import io.github.yotta.logger.manager.adaptor.LocalLogSaveProvider;
import io.github.yotta.logger.manager.advice.LogResponseAdvice;
import io.github.yotta.logger.manager.controller.tenant.NullTenantIdLoader;
import io.github.yotta.logger.manager.controller.tenant.TenantIdLoader;
import io.github.yotta.logger.manager.service.LogService;
import io.github.yotta.logger.message.support.LogSaveProvider;
import io.github.yotta.user.web.autoconfiguration.config.UserWebContextConfiguration;
import io.github.yotta.user.web.autoconfiguration.properties.WhitePathsProperties;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * @author yotta
 * @date 2023/3/26 18:03
 */
@Configuration
@MapperScan("io.github.yotta.logger.manager.dao")
@ComponentScan("io.github.yotta.logger.manager")
public class LoggerManagerAutoConfiguration {


    @Bean
    @ConditionalOnMissingBean({LogResponseAdvice.class})
    public LogResponseAdvice logResponseAdvice() {
        return new LogResponseAdvice() {
        };
    }

    @Bean
    @ConditionalOnMissingBean(TenantIdLoader.class)
    public TenantIdLoader tenantIdLoader() {
        return new NullTenantIdLoader();
    }

    /**
     * 如果类路径下有LogSaveProvider类
     * log-manager提供本地调用的实现
     */
    @ConditionalOnClass(LogSaveProvider.class)
    @Configuration
    static class LocalLogSaveProviderConfiguration {

        @Bean
        @ConditionalOnBean(LogService.class)
        @ConditionalOnMissingBean(LogSaveProvider.class)
        public LogSaveProvider logSaveProvider(LogService logService) {
            return new LocalLogSaveProvider(logService);
        }
    }

    /**
     * 如果类路径下有UserWebContextConfiguration类生效
     * 适配yotta-user-web-starter配置类
     */
    @ConditionalOnClass(UserWebContextConfiguration.class)
    @ConditionalOnBean(UserWebContextConfiguration.class)
    @Configuration
    static class YottaUserWebStarterAdaptorConfiguration implements ApplicationListener<ApplicationReadyEvent> {


        /**
         * 自动添加白名单，添加操作日志接口不需要登录
         */
        @Override
        public void onApplicationEvent(ApplicationReadyEvent event) {
            final ConfigurableApplicationContext applicationContext = event.getApplicationContext();
            final WhitePathsProperties whitePathsProperties = applicationContext.getBean(WhitePathsProperties.class);
            whitePathsProperties.add("/log/admin/add");
        }

    }


}
